package com.iterator;

class LinkedList_ implements Collection_ {
    Node head = null;
    Node tail = null;
    boolean isFirst = true;
    private int size = 0;

    public void add(Object o){
        Node n = new Node(o);
        n.next = null;
        if(head == null){
            head = n;
            tail = n;
        }
        tail.next = n;
        tail = n;
        size ++;
    }

    private class Node{
        private Object o;
        Node next;
        public Node(Object o){
            this.o = o;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "o=" + o +
                    '}';
        }
    }

    public int size(){
        return size;
    }

    @Override
    public Iterator_ iterator() {
        return new LinkedListIterator();
    }

    private class LinkedListIterator implements Iterator_{

        @Override
        public boolean hasNext() {
            if(head.next != null) return true;
            return false;
        }

        @Override
        public Object next() {
            if (isFirst){
                isFirst = false;
                return head;
            }
            head = head.next;
            return head;
        }
    }
}
